package com.zlyx.easy.core.utils;

import java.util.Collection;
import java.util.Map;

import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.zlyx.easy.core.utils.DateUtils;
import com.zlyx.easy.core.utils.ExceptionUtils;
import com.zlyx.easy.core.utils.Logger;
import com.zlyx.easy.core.utils.ObjectUtils;

/**
 * @Auth 赵光
 * @Desc
 * @3018年13月13日
 */
public final class Logger {

	private final static String INFO = "info";

	private final static String DEBUG = "debug";

	private final static String WARN = "warn";

	private final static String ERROR = "error";

	private static int LOG_LEVEL = 0;

	/**
	 * 打印当前系统时间戳
	 */
	public static void currentTime() {
		info(ExceptionUtils.getCurrentClass(3), System.currentTimeMillis());
	}

	/**
	 * 打印当前系统时间
	 */
	public static void date() {
		info(ExceptionUtils.getCurrentClass(3), DateUtils.getNow());
	}

	/**
	 * 打印当前系统时间(带毫秒)
	 */
	public static void dateMs() {
		info(ExceptionUtils.getCurrentClass(3), DateUtils.getNowMs());
	}

	/**
	 * 打印单个对象
	 * 
	 * @param msg
	 */
	public static void info(Object msg) {
		info(ExceptionUtils.getCurrentClass(3), msg);
	}

	/**
	 * 打印键值对
	 * 
	 * @param key
	 * @param value
	 */
	public static void info(Object key, Object value) {
		info(ExceptionUtils.getCurrentClass(3), key, value);
	}

	/**
	 * 打印key:多个对象
	 * 
	 * @param key
	 * @param objs
	 */
	public static void info(Object key, Object... objs) {
		info(ExceptionUtils.getCurrentClass(3), key, JSON.toJSONString(objs));
	}

	/**
	 * 打印集合
	 * 
	 * @param c
	 */
	public static void info(Collection<?> c) {
		info(ExceptionUtils.getCurrentClass(3), JSON.toJSONString(c));
	}

	/**
	 * 打印key:集合
	 * 
	 * @param key
	 * @param c
	 */
	public static <T> void info(Object key, Collection<?> c) {
		info(ExceptionUtils.getCurrentClass(3), key, JSON.toJSONString(c));
	}

	/**
	 * 打印map
	 * 
	 * @param map
	 */
	public static void info(Map<?, ?> map) {
		info(ExceptionUtils.getCurrentClass(3), JSON.toJSONString(map));
	}

	/**
	 * 打印key:map
	 * 
	 * @param key
	 * @param map
	 */
	public static void info(Object key, Map<?, ?> map) {
		info(ExceptionUtils.getCurrentClass(3), key, JSON.toJSONString(map));
	}

	/**
	 * 指定Class打印对象
	 * 
	 * @param declaringClass
	 * @param msg
	 */
	public static void info(Class<?> declaringClass, Object msg) {
		log(declaringClass, Logger.INFO, msg);
	}

	/**
	 * 打印键值对
	 * 
	 * @param key
	 * @param value
	 */
	public static void info(Class<?> declaringClass, Object key, Object value) {
		if (ObjectUtils.isEmpty(key))
			info(declaringClass, value);
		else
			info(declaringClass, key + " -> " + value);
	}

	/**
	 * 打印debug消息
	 * 
	 * @param msg
	 */
	public static void debug(Object msg) {
		debug(ExceptionUtils.getCurrentClass(3), msg);
	}

	/**
	 * 指定Class打印debug消息
	 * 
	 * @param declaringClass
	 * @param msg
	 */
	public static void debug(Class<?> declaringClass, Object msg) {
		log(declaringClass, Logger.DEBUG, msg);
	}

	/**
	 * 打印警告消息
	 * 
	 * @param msg
	 */
	public static void warn(Object msg) {
		warn(ExceptionUtils.getCurrentClass(3), msg);
	}

	/**
	 * 指定Class打印警告消息
	 * 
	 * @param declaringClass
	 * @param msg
	 */
	public static void warn(Class<?> declaringClass, Object msg) {
		log(declaringClass, Logger.WARN, msg);
	}

	/**
	 * 打印err消息
	 * 
	 * @param declaringClass
	 * @param msg
	 */
	public static void err(Object msg) {
		err(ExceptionUtils.getCurrentClass(3), msg);
	}

	/**
	 * 打印异常堆栈消息(不会抛异常)
	 * 
	 * @param e
	 */
	public static void err(Throwable e) {
		err(ExceptionUtils.getCurrentClass(3), e);
	}

	/**
	 * 指定Class打印异常堆栈消息(不会抛异常)
	 * 
	 * @param declaringClass
	 * @param msg
	 */
	public static void err(Class<?> declaringClass, Throwable e) {
		err(declaringClass, ExceptionUtils.getExceptionMsg(e));
	}

	/**
	 * 指定Class打印err消息
	 * 
	 * @param declaringClass
	 * @param msg
	 */
	public static void err(Class<?> declaringClass, Object msg) {
		log(declaringClass, Logger.ERROR, msg);
	}

	public static void log(Class<?> cls, String type, Object msg) {
		if (DEBUG.equals(type) && LOG_LEVEL <= 1) {
			LoggerFactory.getLogger(cls).debug("{}", msg);
		}
		if (INFO.equals(type) && LOG_LEVEL <= 2) {
			LoggerFactory.getLogger(cls).info("{}", msg);
		}
		if (WARN.equals(type) && LOG_LEVEL <= 3) {
			LoggerFactory.getLogger(cls).warn("{}", msg);
		}
		if (ERROR.equals(type)) {
			LoggerFactory.getLogger(cls).error("{}", msg);
		}
	}

	public static void setLevel(int level) {
		Logger.LOG_LEVEL = level;
	}

	public static void setOpen(boolean open) {
	}

}
